#!/bin/ksh
#
# GenericSubdir -- create generic lsof 4.x distribution subdirectory
#
# Usage: GenericSubdir <dialect> [<suffix>]
#
#	<dialect>	dialect's subdirectory name
#
#	[<suffix>]	optional suffix for version number

cd $(dirname $0)/..

# Get version number.

V=`sed '/VN/s/.ds VN \(.*\)/\1/' version`
if test $? -ne 0
then
  echo $V
  exit 1
fi

# Handle arguments.

if test $# -lt 1 -o $# -gt 2
then
  echo "Usage: <dialect> [<suffix>]"
  exit 1
fi
DIALECT=$1
if test $# -eq 2
then
  V=${V}$2
fi

# Set variable names.

DIR=lsof_${V}.${DIALECT}
F=
F="$F 0..README.BEFORE.README.FIRST README.md check.bash"
F="$F 00.README.FIRST 00CREDITS 00DCACHE 00DIALECTS 00DIST 00FAQ 00LSOF-L 00PORTING 00QUICKSTART 00README 00TEST 00XCONFIG"
F="$F AFSConfig ChangeLog Configure Customize Inventory"
F="$F arg.c lsof_fields.h lsof.h main.c misc.c node.c print.c proc.c proto.h regex.h store.c usage.c util.c"
F="$F version"
MS=Lsof.8
MD=lsof.8
MF=lsof.man

# Prepare for premature exit.

trap 'rm -rf $DIR; exit' 1 2 3 15

# Make a temporary directory and copy the relevant files to it.

rm -rf $DIR
mkdir $DIR
echo "Copying:\c"
for i in $F
do
  echo " $i\c"
  cp -r $i $DIR
done
echo " ... done"
echo "Copying lib ...\c"
cp -r lib $DIR/lib
echo " done"
echo "Removing extraneous stuff from lib ...\c"
rm -rf $DIR/lib/RCS $DIR/lib/OLD $DIR/lib/NEW
rm -rf $DIR/lib/Makefile $DIR/lib/*.[oa]
echo " done"
mkdir $DIR/dialects
echo "Copying dialects/${DIALECT} ...\c"
cp -r dialects/${DIALECT} $DIR/dialects
echo " done"
echo "Removing extraneous stuff from dialects/${DIALECT} ...\c"
rm -rf $DIR/dialects/${DIALECT}/distfile.kvm
rm -rf $DIR/dialects/${DIALECT}/Makefile.LOCAL
rm -rf $DIR/dialects/${DIALECT}/version.h
find ${DIR}/dialects -name OLD -exec rm -rf {} \; > /dev/null 2>&1
find ${DIR}/dialects -name NEW -exec rm -rf {} \; > /dev/null 2>&1
find ${DIR}/dialects -name support -exec rm -rf {} \; > /dev/null 2>&1
find ${DIR}/dialects -name RCS -exec rm -rf {} \; > /dev/null 2>&1
echo " done"
echo "Copying scripts ...\c"
cp -r scripts $DIR
echo " done"
echo "Removing extraneous stuff from scripts ...\c"
rm -rf $DIR/scripts/NEW $DIR/scripts/OLD $DIR/scripts/RCS
echo " done"
echo "Copying tests ...\c"
cp -r tests $DIR
echo " done"
echo "Removing extraneous stuff from tests ...\c"
rm -rf $DIR/tests/NEW $DIR/tests/OLD $DIR/tests/RCS $DIR/tests/*.o
rm -rf $DIR/tests/00README
rm -rf $DIR/tests/config.* $DIR/tests/LTbasic $DIR/tests/LTbigf
rm -rf $DIR/tests/LTdnlc $DIR/tests/LTlock $DIR/tests/LTnfs
rm -rf $DIR/tests/LTnlink $DIR/tests/LTsock $DIR/tests/LTszoff
rm -rf $DIR/tests/LTunix
echo " done"
echo "Producing $MD and $MF ...\c"
soelim < $MS > $DIR/$MD
nroff -man $MS | colcrt - | cat -s > $DIR/$MF
echo " done"
echo "Producing 00MANIFEST ...\c"
(cd $DIR; ls -FR > 00MANIFEST)
echo " done"
